package br.ebula.apl.controladores;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.ebula.apl.dto.DTOIncluiBula;
import br.ebula.apl.fachada.FachadaIncluiBula;

public class ServletIncluiBula extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6660152525478703836L;

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		doGet(req,resp);
	}
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp){
		
		DTOIncluiBula dto = new DTOIncluiBula();
		dto.retiraConteudoBula(req);
		
		FachadaIncluiBula fachada = new FachadaIncluiBula();
		fachada.incluiBula(dto);
		
		if(dto.hasErro()){
			try {
				req.setAttribute("DTOIncluiBula", dto);
				RequestDispatcher r = req.getRequestDispatcher("apresentacao/inclui.jsp");
				r.forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else{
			req.setAttribute("mensagem", "Bula incluida com sucesso!");
			ServletListaBulas s = new ServletListaBulas();
			s.doPost(req, resp);
		}
	}

}
